Page({
    data: {
        rules: [],
        alertShow: false, 
        stuid: null, //学号
        password: null, //密码
        bindShow:false,
        loginShow: false,
    },
    onLoad: function (options) {
        console.log('**登录页**');
        const that = this;
        // 确定是登录页面还是绑定页面
        wx.getStorage({
            key: 'user_done',
            success: function(res) {
                console.log(res);
                if(res.data == 'none'){
                    that.setData({
                        loginShow:true0
                    });
                }else{
                    that.setData({
                        bindShow: true
                    });
                }
            }
        });
    },
    bindStuidInput: function (e) {
        // 学号输入
        console.log(e);
        this.setData({
        stuid: e.detail.value
        });
    },
    bindPasswordInput: function (e) {
        // 密码输入
        this.setData({
        password: e.detail.value
        });

    },
    bindBind: function () {
        
        // 进行登录
        const that = this;
        if (that.data.stuid == null || that.data.password == null){
        wx.showModal({
            title: '提示',
            content: '学号或密码不能为空',
            showCancel: false,
            success: function (res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                }
            }
        });
            return false;
        }
        
        var _t = (new Date()).getTime();

        wx.showLoading({
            title: '登录中',
        });
        console.log('--------缓存的openid-----------');
        console.log(wx.getStorageSync('openid'));
        if (wx.getStorageSync('openid')){
            wx.request({
                url: 'https://2016.bookgo.com.cn/wxapp/index/bind?time=_t',
                data: {
                    openid: wx.getStorageSync('openid'),
                    stuid: that.data.stuid,
                    password: that.data.password,
                    aid: 3
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
                    wx.hideLoading();
                    console.log('-------缓存的openid存在---wxapp/index/bind--->res--------------');
                    console.log(res);
                    switch (res.data.code) {
                        case 101:
                            that.go_page(res.data.data);
                            break;
                        case 102:
                            that.setData({
                                bindShow: true,
                                loginShow: false
                            });
                            break;
                        case 103:
                            // 学号和密码不匹配
                            wx.showModal({
                                title: '提示',
                                content: res.data.data,
                                showCancel: false,
                                success: function (res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定')
                                    }
                                }
                            });
                            break;
                        case 104:
                            // 服务器错误
                            wx.showModal({
                                title: '提示',
                                content: res.data.data,
                                showCancel: false,
                                success: function (res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定')
                                    }
                                }
                            });
                            break;
                        default:
                            console.log(res.data)
                    }
                },
                fail: function (res) {
                    console.log(res);
                },
                complete: function () {
                    console.log('登录结束')
                }
            })
        }else{
            wx.login({
                success: function (e) {
                    wx.request({
                        url: 'https://2016.bookgo.com.cn/wxapp/index/login',
                        data: {
                            code: e.code
                        },
                        header: {
                            'content-type': 'application/json'
                        },
                        success: function (res) {
                            console.log('+++++++++++获取openid++++++++++++++++');
                            console.log(res.data)
                            wx.setStorageSync('openid', res.data.userinfo.openid)
                            var openid = res.data.userinfo.openid;
                            wx.request({
                                url: 'https://2016.bookgo.com.cn/wxapp/index/bind?time=_t',
                                data: {
                                    openid: openid,
                                    stuid: that.data.stuid,
                                    password: that.data.password,
                                    aid: 3
                                },
                                header: {
                                    'content-type': 'application/json'
                                },
                                success: function (res) {
                                    wx.hideLoading();
                                    console.log('------缓存的openid不存在----wxapp/index/bind--->res--------------');
                                    console.log(res);
                                    switch (res.data.code) {
                                        case 101:
                                            that.go_page(res.data.data);
                                            break;
                                        case 102:
                                            that.setData({
                                                bindShow: true,
                                                loginShow: false
                                            });
                                            break;
                                        case 103:
                                            // 学号和密码不匹配
                                            wx.showModal({
                                                title: '提示',
                                                content: res.data.data,
                                                showCancel: false,
                                                success: function (res) {
                                                    if (res.confirm) {
                                                        console.log('用户点击确定')
                                                    }
                                                }
                                            });
                                            break;
                                        case 104:
                                            // 服务器错误
                                            wx.showModal({
                                                title: '提示',
                                                content: res.data.data,
                                                showCancel: false,
                                                success: function (res) {
                                                    if (res.confirm) {
                                                        console.log('用户点击确定')
                                                    }
                                                }
                                            });
                                            break;
                                        default:
                                            console.log(res.data)
                                    }
                                },
                                fail: function (res) {
                                    console.log(res);
                                },
                                complete: function () {
                                    console.log('登录结束2')
                                }
                            })
                        },

                    })
                }
            })
        }

    },
    wx_app_bind: function () {
        // 检查是否公众号也绑定了
        const that = this;
        var _t = (new Date()).getTime();
        wx.showLoading({
        title: '确认中',
        });
        wx.request({
        url: 'https://2016.bookgo.com.cn/wxapp/index/wx_app_bind?time=_t',
        data: {
            openid: wx.getStorageSync('openid'),
            aid:3
        },
        header: {
            'content-type': 'application/json'
        },
        success: function (res) {
            wx.hideLoading();
            console.log('---------wxapp/index/wx_app_bind----->res-----')
            console.log(res.data)
            if (res.data.code == 101) {
            wx.showToast({
                title: '绑定成功！',
                icon: 'success',
                duration: 2000
            })
            // 这里顺带将其绑定的信息记录下来 
            that.go_page('user', res.data.data);
            } else {
            // 没有在公众号绑定
            wx.showModal({
                title: '提示',
                content: '还未完成公众号绑定',
                showCancel: false,
                success: function (res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                }
                }
            });
            }
        }
        })
    },
    go_page:function(user){
        wx.setStorageSync('user', user);
        wx.setStorageSync('user_done', 'yes');
        wx.redirectTo({
            url: wx.getStorageSync('redirect')
            // url:'/bookgo/fan/ifan/ifan'
        });
    }
});
